package com.cavy.platform.server.service;

import com.cavy.platform.server.domain.WorkSpace;
import com.cavy.platform.server.dto.WorkSpaceCreateDto;
import com.cavy.platform.server.dto.WorkSpaceDeleteDto;
import com.cavy.platform.server.dto.WorkSpaceDto;
import com.cavy.platform.server.mapper.WorkSpaceMapper;
import com.cavy.platform.server.repository.WorkSpaceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * author: 唐旭
 */
@Service
public class WorkSpaceService {
    @Autowired
    private WorkSpaceRepository repository;

    public void create(WorkSpaceCreateDto createDto) {
        repository.create(WorkSpaceMapper.INSTANCE.convertTo(createDto));
    }

    public void delete(WorkSpaceDeleteDto name) {
        repository.delete(name.getName());
    }

    public List<WorkSpaceDto> list() {
        List<WorkSpace> spaces = repository.getList();
        List<WorkSpaceDto> result = new ArrayList<>(spaces.size());
        for (WorkSpace space : spaces) {
            result.add(WorkSpaceMapper.INSTANCE.converToSpaceDto(space));
        }
        return result;
    }
}
